package com.itheima.pinda;

import com.itheima.j2cache.anno.EnableCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
@EnableSwagger2
// @Import(CacheMethodInterceptor.class)   // 使用这个@Import注解, 实现SpringBoot启动类扫描非启动类包下面的类, 注入到容器中
@EnableCache    // 标准做法 -->  把@Import改造成@EnableCache规范做法
public class UserApplication {
    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext context = SpringApplication.run(UserApplication.class, args);
        // 启动完成后, 在控制台提示项目启动成功, 并且输出当前服务对应的swagger接口文档地址
        // environment 可以获取配置文件中的信息
        ConfigurableEnvironment env = context.getEnvironment();
        String appName = env.getProperty("spring.application.name");
        // 会获得局域网的ip, 会不准确
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        // 会从配置中心 nacos中获得
        String port = env.getProperty("server.port");

        String protocol = "http";
        log.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t{}://localhost:{}\n\t"
                        + "External: \t{}://{}:{}\n\t"
                        + "Swagger: \t{}://localhost:{}/doc.html\n\t"
                        + "Profile(s): \t{}\n----------------------------------------------------------",
                appName, protocol, port, protocol, hostAddress, port, protocol, port, env.getActiveProfiles());
    }

}